package com.itheima.kafka.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController //注册到 ioc容器中
public class HelloController {

    //引入 kafka
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    //传递 字符串
    @GetMapping("/hello")
    public String hello(){
        //第一个参数：topics  
        //第二个参数：消息内容             // 发送信息
        kafkaTemplate.send("kafka-hello","黑马程序员");
        return "ok";
    }

    //传递 对象
    @GetMapping("hi")
    public String hi(){
        //创建 map
        Map<String, String> map = new HashMap<>();
        map.put("A","111111111");
        map.put("B","222222222");
        //转换为 字符串
        String s = JSON.toJSONString(map);
        kafkaTemplate.send("kafka-hi",s);
        return "ok";
    }

}